import   java.sql.*;   

public class DataBase {
	private Connection DB_Conn;
	private Statement stmt;  
    private ResultSet rs;
	private String DB_driver;
	private String DB_url_base;
	private String DB_name;
	private String DB_user;
	private String DB_password;
	private String DB_url;
	private String result="";
	
	
	public DataBase(String driver,String url_base,String name,String user,String password){
		this.DB_driver=driver;
		this.DB_url_base=url_base;
		this.DB_name=name;
		this.DB_user=user;
		this.DB_password=password;
		this.DB_url=DB_url_base+DB_name;
	}
	
	public Connection DBConnection(){
		try{   
	        Class.forName(DB_driver).newInstance();
	     
	        DB_Conn=DriverManager.getConnection(DB_url, DB_user, DB_password);
		}catch(Exception ex) {
			// handle any errors 
	        System.out.println("Failed to get connection: " + ex.getMessage()); 
	    }
		return DB_Conn;
	}
	
	public String excuteQuery(String SQL){
		String user_name;
		try	{
			stmt=this.DBConnection().createStatement();
			rs = stmt.executeQuery(SQL);
			while(rs.next()){
				user_name=rs.getString("UserName");
				result+=user_name+"#";
			}
		}catch(Exception ex) {
				// handle any errors 
		        System.out.println("Failed to get query: " + ex.getMessage()); 
		    }
		return result;
	}
	
	/* public   static   void     main(String   args[]){   
			String User_name="aa";
			String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
			String url_base="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=";
			String db_name="XUANKE";
			String name="sa";
			String password="";
			
			DataBase database = new DataBase(driver,url_base,db_name,name,password);
			
			String SQL="select UserName from Users where UserName like '"+User_name+"%' order by UserName";  
			String result = database.excuteQuery(SQL); 
			System.out.println(result);
	  }   
	  */
}
		
		
		
	
	
	


